//
//  KLScreenVideoCell1.m
//  kularkDemo
//
//  Created by szblsx2 on 2020/4/9.
//  Copyright © 2020 szblsx. All rights reserved.
//

#import "KLScreenVideoCell1.h"
#import "KLLocalVideoCell.h"
#import "KLRemoteVideoCell.h"

@interface KLScreenVideoCell1 ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, assign) BOOL focus;

@end

@implementation KLScreenVideoCell1

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupViewsFrame:frame];
    }
    return self;
}


- (void)setupViewsFrame:(CGRect)frame
{
    UICollectionView *videoRenderer = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    videoRenderer.scrollEnabled = NO;
    [videoRenderer registerClass:[KLLocalVideoCell class] forCellWithReuseIdentifier:kLocalVideoCellIdentifier];
    [videoRenderer registerClass:[KLRemoteVideoCell class] forCellWithReuseIdentifier:kRemoteVideoCellIdentifier];
    videoRenderer.dataSource = self;
    videoRenderer.delegate = self;
    [self addSubview:videoRenderer];
    
    _collectionView = videoRenderer;
}

- (void)reloadCellWithRemoteVideoTracks:(NSArray *)dataArray focus:(BOOL)focus
{
    _focus = focus;
    _dataArray = dataArray;
    
    [self reloadVideoRender];
}

- (void)reloadVideoRender{
    __weak __typeof(self) ws = self;
    [UIView animateWithDuration:0 animations:^{
        [ws.collectionView reloadData];
    }];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        KLLocalVideoCell *localCell = [collectionView dequeueReusableCellWithReuseIdentifier:kLocalVideoCellIdentifier forIndexPath:indexPath];
        localCell.openLocalVideo = self.openLocalVideo;
        [localCell reloadCellWithLocalUser:self.dataArray[indexPath.row] focus:self.focus];
        return localCell;
    }else{
        KLRemoteVideoCell *remoteCell = [collectionView dequeueReusableCellWithReuseIdentifier:kRemoteVideoCellIdentifier forIndexPath:indexPath];
        [remoteCell reloadCellWithRemoteUser:self.dataArray[indexPath.row] focus:self.focus];
        return remoteCell;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat cWidth = collectionView.frame.size.width;
    CGFloat cHeight = collectionView.frame.size.height;
//    if (self.dataArray.count == 1) {
//        return CGSizeMake(cWidth, cHeight);
//    }else if (self.dataArray.count == 2){
//        return CGSizeMake(cWidth, cHeight/2);
//    }
    return CGSizeMake(cWidth/2, cHeight/2);
}

#pragma mark - Getter

- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

@end
